﻿using System;
using CosyTrade.Data;
using CosyTrade.Distribution;
using CosyTrade.Organization;
using CosyTrade.UI;

namespace CosyTrade.ClientForm.Distribution
{
    public partial class OrderPayLogDetail : DetailFormBase
    {
        public OrderPayLogDetail(): this(Guid.Empty) {}
        public OrderPayLogDetail(Guid extKey) : base(extKey) 
        {
            InitializeComponent();
        }

        public Order CurrentOrder;
       
        protected override BusinessObject InitBusinessObject(Guid currObjKey)
        {
            return Operation.FinancialSystem.GetExpenditureDetail(currObjKey);
        }

        protected override BusinessObject CreateNewBusinessObject() 
        {
            return new OrderPayLog();
        }

        protected override string GetTitle() 
        {
            return "订单付款记录";
        }

        protected override void DataBinding() 
        {
            BindingControl(popEditHandler, "EditValue", "GetPayer");
            BindingControl(txtPayMeney, "EditValue", "PayMeney");
            BindingControl(datePayDate, "EditValue", "PayDate");
        }

        protected override bool IsVail()
        {
            if (popEditHandler.EditValue == null)
            {
                Functions.ShowMessageBox("请输入收款人");
                return false;
            }

            return true;
        }

        protected override DetailFormCurrentStat GetDefaultStat()
        {
            return DetailFormCurrentStat.ViewStat;
        }

        protected override void Save(BusinessObject currentObj)
        {
            if (CurrentOrder == null) return;
            if (!(currentObj is OrderPayLog)) return;
            OrderPayLog opl = currentObj as OrderPayLog;
            opl.OrderGuid = CurrentOrder.PrimaryKey;
            CurrentOrder.PayStat = OrderPayStat.NonPayAll;
            if((bool)checkEdit1.EditValue) CurrentOrder.PayStat = OrderPayStat.PayAll;
            switch (CurrentStat)
            {
                case DetailFormCurrentStat.AddStat:
                    Operation.DistrbutionSystem.CreatePayLog(CurrentOrder, opl);
                    break;
                case DetailFormCurrentStat.EditStat:
                    Operation.DistrbutionSystem.UpdatePayLog(opl);
                    break;
                default:
                    break;
            }
        }

        private void SelectEmployee1OnSelected(object sender, EventArgs e)
        {
            popEditHandler.EditValue = selectEmployee1.GetSelectedEmployee;
            if (popupContainerControl1.OwnerEdit != null)
                popEditHandler.ClosePopup();
        }

        private void popupContainerEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (e.Value == null)
                e.DisplayText = "未指定";
            else
            {
                Employee employee = e.Value as Employee;
                e.DisplayText = employee != null ? employee.EmpName : "未指定";
            }
        }

    }
}